home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Floating point calculation order
- Date: Tue, 23 Jan 96 20:13:58 GMT
- Organization: none
- Message-ID: <822428038snz@genesis.demon.co.uk>
- References: <m0tedv8-0002eqC@sice.nsk.su> <3104c6d9.134061184@nntp.ix.netcom.com> <DLnE5K.2xH@microunity.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <DLnE5K.2xH@microunity.com> toms@MicroUnity.com "Tom Sanders" writes:
-
- >In article <3104c6d9.134061184@nntp.ix.netcom.com>, miker3@ix.netcom.com (Mike
- > Rubenstein) writes:
- >|> "Pavel A. Zemtsov" <PZEM@sice.nsk.su> wrote:
- >|>
- >|> >
- >|> > Having declaration
- >|> >
- >|> > double x, p, q, r;
- >|> >
- >|> > my compiler (cc on SCO 3.2) calculated following expression:
- >|> >
- >|> > x = p * q / r;
- >|> >
- >|> > as p * (q/r); (I mean, it divided first, than multiplied). That
- >|> > resulted in precision loss.
- >|> >
- >|> > Is this legal behavior?
- >|>
- >|> Not in ANSI C, but it was legal in K&R. If memory serves, it was not
- >|> made illegal until very late in the standardizattion process so if
- >|> your compiler was written before the standard was finalized it may not
- >|> adhere to this.
- >|>
- >|> Michael M Rubenstein
- >
- >I am a firm believer in not allowing a compiler to make these decisions for
- >me. It's best to specify with parens the order of evaluation and not leave
- >it up to the compiler defaults. I would actually prefer a compiler that
- >gave a warning if I did have an equation that could be evaluated in more
- >than 1 order (something I have not done in over 20 years).
-
- Parentheses in C allow you to change the operator/operand grouping within
- expressions. They don't enforce any order of evaluation beyond that implied
- by the grouping. So:
-
- x = p * q / r;
-
- and
-
- x = (p * q) / r;
-
- mean *exactly* the same thing in C since the parentheses don't change the
- grouping.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-